// #ifdef APP-PLUS  
//检测接口
const requestUrl = 'https://dev.szsti.org/cockpit/api/user/versionUpdate';
//苹果商店app链接
// const appstoreUrl = 'itms-apps://itunes.apple.com/cn/app/promoketo/id1478098035?mt=8';
export default function() {
    // 设备系统平台--安卓||ios
    var platform = uni.getSystemInfoSync().platform;
    plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
        uni.request({
            url: requestUrl,
            data: {
                "app_version": widgetInfo.version, //"1.0.0"
                "app_id": "__UNI__2E99E70" //"__UNI__2E99E70" || widgetInfo.appid
            },
            method: "POST",
            success: (result) => {
                // 是否有更新，更新的类型和资源地址
                // update:true||false
                // type:"pkg"整包更新||"wgt" 热更新
                // source_path:资源链接

                console.log('检查跟新返回-》', result)
                if (result.data.status != 200) return;
                var data = result.data.data;
                if (data.update) {

                    // 跟新类型
                    switch (data.type) {
                        case "pkg":
                            uni.showModal({
                                title: '提示',
                                content: '程序有新版本,是否立即更新？',
                                confirmText: '更新',
                                success: function(res) {
                                    if (res.confirm) {
                                        if (platform === "android") {
                                            uni.downloadFile({
                                                url: data.source_path,
                                                success: (downloadResult) => {
                                                    if (downloadResult.statusCode === 200) {
                                                        plus.runtime.install(downloadResult.tempFilePath, {
                                                            force: false
                                                        }, function() {
                                                            console.log('install success...');
                                                            plus.runtime.restart();
                                                        }, function(e) {
                                                            console.error('install fail...');
                                                            uni.showToast({
                                                                title: '安装失败'
                                                            });
                                                        });
                                                    } else {
                                                        uni.showToast({
                                                            title: '更新失败'
                                                        });
                                                    }
                                                }
                                            });
                                        } else {
                                            plus.runtime.openURL(appstoreUrl)
                                            // itms-apps://itunes.apple.com/cn/app/qq/id123456789?mt=8
                                            // itms-apps://itunes.apple.com/cn/app/+app名字+/id+appleID+?mt=8
                                        }
                                    }
                                }
                            });

                            break;


                        case "wgt":
                            uni.showModal({
                                title: '提示',
                                content: '程序有资源更新',
                                confirmText: '更新',
                                success: function(res) {
                                    if (res.confirm) {
                                        uni.downloadFile({
                                            url: data.source_path,
                                            success: (downloadResult) => {
                                                if (downloadResult.statusCode === 200) {
                                                    plus.runtime.install(downloadResult.tempFilePath, {
                                                        force: false
                                                    }, function() {
                                                        console.log('install success...');
                                                        plus.runtime.restart();
                                                    }, function(e) {
                                                        console.error('install fail...', e);
                                                    });
                                                }
                                            }
                                        });
                                    }
                                }
                            })

                            break;
                    }
                }
            }
        });
    });
}
// #endif
